/**
 * CS202 - Spring 2009
 * Professor: Dr. Michael Peterson
 * Student: Deylo Woo (dwoo711@gmail.com)
 */
package project_3;

import java.awt.*;       // Old library classes, you still need them
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Deylo
 */
public class GameMain extends JFrame implements KeyListener {

    private int _height = 810;
    private int _width = 810;
    private GamePanel _gamePnl;
    private ControlPanel _controlPnl;

    public GameMain() {
        super("Game v1.0");
        addKeyListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(_width, _height);
        setLayout(new BorderLayout(5, 5));

        //Add panels
        _gamePnl = new GamePanel(800, 700);
        _gamePnl.setBounds(new Rectangle(0, 0, 800, 700));
        add(_gamePnl, BorderLayout.CENTER);
        add(_gamePnl);

        _controlPnl = new ControlPanel();
        _controlPnl.setBounds(new Rectangle(0, 0, 800, 100));
        add(_controlPnl, BorderLayout.SOUTH);
        add(_controlPnl);

        center(this);
        setVisible(true);

        while (true) {
            pauseGame();

            _gamePnl.Move();
            _gamePnl.repaint();

            String s = ("testing" + (Integer) _gamePnl.getCannon().getXCoord()).toString();
            _controlPnl.UpdateMessage(s);
        //_controlPnl.repaint();
        }

    }

    public static void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();

        int w = frame.getWidth();
        int h = frame.getHeight();

        int x = center.x - w / 2, y = center.y - h / 2;
        frame.setBounds(x, y, w, h);
        frame.validate();
    }

    private static void pauseGame() {
        try {
            Thread.sleep(20); // pause for 20 ms
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }

    /**
     * @return the _gamePnl
     */
    public GamePanel getGamePnl() {
        return _gamePnl;
    }

    /**
     * @return the _controlPnl
     */
    public ControlPanel getControlPnl() {
        return _controlPnl;
    }

    public void keyTyped(KeyEvent e) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == e.VK_SPACE) {
            _gamePnl.FireMissile();
        }
        if (e.getKeyCode() == e.VK_LEFT) {
            _gamePnl.MoveLeft();
        } else if (e.getKeyCode() == e.VK_RIGHT) {
            _gamePnl.MoveRight();
        }
    }

    public void keyReleased(KeyEvent e) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    public static void main(String[] args) {
        GameMain _newGame = new GameMain();

    }
}
